class Compiler {
    constructor(vm) {
        this.el = vm.$el
        this.vm = vm
        this.compile(this.el)
    }
    // 编译模板
    compile(el) {
        let childNodes = Array.from(el.childNodes)
        childNodes.forEach(node => {
            if (this.isTextNode(node)) {
                this.compileText(node)
            } else if (this.isElementNode(node)) {
                this.compileElement(node)
            }
            // node存在子节点
            if (node.childNodes && node.childNodes.length) {
                this.compile(node)
            }
        })
    }
    // 编译元素节点，处理指令
    compileElement(node) {
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name
            //事件处理   或 @  
            if (attrName.startsWith('@')) { //@
                attrName = attrName.substr(1)
                let fn = attr.value
                this.compileEvent(node, fn, attrName)
            } else if (attrName.startsWith('v-on:')) { // v-on:
                attrName = attrName.substr(5)
                let fn = attr.value
                this.compileEvent(node, fn, attrName)
            } else if (this.isDirective(attrName)) { // 编译指令 v-
                attrName = attrName.substr(2)
                let key = attr.value
                this.update(node, key, attrName)
            }
        })
    }
    update(node, key, attrName) {
        let updateFn = this[attrName + 'Update']

        updateFn && updateFn.call(this, node, this.vm[key], key)
    }
    textUpdate(node, val, key) {
        node.textContent = val
        new Watcher(this.vm, key, newVal => {
            node.textContent = newVal
        })
    }
    modelUpdate(node, val, key) {
        node.value = val
        new Watcher(this.vm, key, newVal => {
            node.value = newVal
        })
        node.addEventListener('input', () => {
            this.vm[key] = node.value
        })
    }
    // v-html
    htmlUpdate(node, val, key) {
        node.innerHTML = val
        new Watcher(this.vm, key, newVal => {
            node.innerHTML = newVal
        })
    }
    // 编译文本节点 处理差值表达式
    compileText(node) {

        const reg = /\{\{(.+?)\}\}/g
        let value = node.textContent
        if (reg.test(value)) {
            let key = RegExp.$1.trim()
            let newValue = this.vm
            key.split('.').forEach(key => {
                newValue = newValue[key]
            })

            node.textContent = value.replace(reg, this.text(newValue))
            new Watcher(this.vm, key, newVal => {
                console.log('text update')
                console.log(key)

                node.textContent = this.text(newVal)
            })
        }
    }
    // 
    compileEvent(node, fn, eventType) {
        node.addEventListener(eventType, () => {
            this.vm.methods[fn] && this.vm.methods[fn].call(this.vm)
        })
    }
    // 判断元素的属性是否是指令
    isDirective(attrName) {
        return attrName.startsWith('v-')
    }
    // 判断元素的属性是否是事件 v-on
    isEventDirective(attrName) {
        return attrName.startsWith('v-on:') || attrName.startsWith('@')
    }
    // 判断节点是否是元素节点
    isElementNode(node) {
        return node.nodeType === 1
    }
    // 判断节点是否是文本节点
    isTextNode(node) {
        return node.nodeType === 3
    }
    text(val) {
        return typeof val === 'string' ? val : JSON.stringify(val)
    }
}